//
//  Global.m
//  jinghui
//
//  Created by 史宵宵 on 2017/5/16.
//  Copyright © 2017年 史宵宵. All rights reserved.
//

#import "Global.h"

@implementation Global
+ (AppDelegate *)appdelegete
{
    return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
+ (UIColor *)colorWithHexValue:(NSUInteger)hexValue alpha:(CGFloat)alpha
{
    return [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0
                           green:((float)((hexValue & 0xFF00) >> 8))/255.0
                            blue:((float)(hexValue & 0xFF))/255.0
                           alpha:alpha];
}
//保存个人登录token
+ (void)setUserTokenInfo:(NSString*)userTokenInfoDic
{
    NSUserDefaults *userDefacult = [NSUserDefaults standardUserDefaults];
    [userDefacult setObject:userTokenInfoDic forKey:KUSERTOKEN];
    [userDefacult synchronize];
}
//读取个人登录token
+ (NSString*)getUserTokenInfo
{
    return (NSString*)[[NSUserDefaults standardUserDefaults] objectForKey:KUSERTOKEN];
}


//设置状态栏颜色
+ (void)setStatusBarBackgroundColor:(UIColor *)color {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}
// 获取指定最大宽度、最大高度、字体大小的string的size
+ (CGSize)getSizeOfString:(NSString *)string maxWidth:(float)width maxHeight:(float)height withFontSize:(CGFloat)fontSize
{
    //UIFont *font = [UIFont systemFontOfSize:fontSize];
    CGSize size =  [string boundingRectWithSize:CGSizeMake(width, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:fontSize]} context:nil].size;
    return size;
}
//弹框
+ (void)showActivityWithView:(UIView *)showView withText:(NSString *)text
{
    MBProgressHUD *progressHUD = [MBProgressHUD showHUDAddedTo:showView animated:NO];
    progressHUD.mode=MBProgressHUDModeText;
    progressHUD.label.text = text;
    progressHUD.alpha=0.8;
    progressHUD.label.font=[UIFont boldSystemFontOfSize:14];
    progressHUD.mj_y = - 90;
//    progressHUD.yOffset=-90;
    [progressHUD hideAnimated:YES afterDelay:1.0];
}
/**
 *	@brief	正则校验-手机号码---所有类型的电话号码
 *
 *	@param 	mobile 	输入手机号码
 *
 *	@return	返回是否合法手机号码（BOOL）
 */
+ (BOOL)validateMobile:(NSString *)mobile
{
    //验证手机号格式
    NSString *MOBILE = @"^((13[0-9])|(15[^4,\\D])|(14[5,7])|(17[0,6-8])|(18[0-9]))\\d{8}$";
    NSPredicate *regextestmobile=[NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    BOOL res = [regextestmobile evaluateWithObject:mobile];
    if (res)
    {
        return YES;
    }
    else
    {
        return NO;
    }
}
//修改字符串行高
+ (NSAttributedString*)returnAttributedstringFromstring:(NSString *)string andlineHeight:(CGFloat)lineHeight
{
    NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
    paragraphStyle.maximumLineHeight = lineHeight;
    paragraphStyle.minimumLineHeight = lineHeight;
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    [attributes setObject:paragraphStyle forKey:NSParagraphStyleAttributeName];
//    CGFloat baselineOffset = (lineHeight - label.font.lineHeight) / 4;
//    [attributes setObject:@(baselineOffset) forKey:NSBaselineOffsetAttributeName];
    
    return [[NSAttributedString alloc] initWithString:string attributes:attributes];
    
}
//修改字符串的颜色或及大小
+ (NSMutableAttributedString*)returnAttributedstringFromstring:(NSString *)string andlocation:(NSInteger)location andLength:(NSInteger)length andAttributeStringColor:(UIColor *)color andFondSize:(NSInteger)size
{
    NSMutableAttributedString *resultString=[[NSMutableAttributedString alloc]initWithString:string];
    [resultString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:size] range:NSMakeRange(location, length)];
    
    [resultString addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(location, length)];
    
    return resultString;
    
}
//获取当前时间的字符串
+ (NSString *)timeStamp
{
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [formatter stringFromDate:datenow];
    // 时间转时间戳的方法:
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]];
    return timeSp;
}
//字符串转换成时间
+ (NSDate *)dateFromDateString:(NSString *)dateString
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //    [formatter setTimeZone:[NSTimeZone defaultTimeZone]];
    
    NSDate *date=[formatter dateFromString:dateString];
    return date;
}
//时间字符串转HH:mm
+ (NSString *)callBackTime:(NSString *)timeString
{
    //获得当前时间
    NSDate *currentDate=[NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
    NSTimeInterval  timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];
    currentDate = [currentDate dateByAddingTimeInterval:timeZoneOffset];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strFirst = [NSString stringWithFormat:@"%ld", (long)[currentDate timeIntervalSince1970]];
    int firstTimeStamp=[strFirst intValue];
    
    NSString *dateTime = [Global timeWithTimeIntervalString:timeString];
    //获取服务器返回时间
    NSDate *callBackdate =[dateFormatter dateFromString:dateTime];
    callBackdate = [callBackdate dateByAddingTimeInterval:timeZoneOffset];
    NSString *strSecond = [NSString stringWithFormat:@"%ld", (long)[callBackdate timeIntervalSince1970]];
    int secondTimeStamp=[strSecond intValue];
    
    int datePoor = firstTimeStamp-secondTimeStamp;
    
    if (datePoor<0)
    {
        dateTime=@"刚刚";
    }
    else if (0<=datePoor && datePoor<60)
    {
        dateTime=[NSString stringWithFormat:@"%d秒前",datePoor];
    }
    else
    {
        dateTime = [dateTime substringWithRange:NSMakeRange( 11, 5)];
    }
    return dateTime;
}
//获取俩个时间是时间差
+ (int)callDatePoor:(NSString *)timeString{
    //获得当前时间
    NSDate *currentDate=[NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
    NSTimeInterval  timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];
    currentDate = [currentDate dateByAddingTimeInterval:timeZoneOffset];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *strFirst = [NSString stringWithFormat:@"%ld", (long)[currentDate timeIntervalSince1970]];
    int firstTimeStamp=[strFirst intValue];
    
    NSString *dateTime = timeString;
    //    NSString *dateTime = [Global timeWithTimeIntervalString:timeString];
    //获取服务器返回时间
    NSDate *callBackdate =[dateFormatter dateFromString:dateTime];
    callBackdate = [callBackdate dateByAddingTimeInterval:timeZoneOffset];
    NSString *strSecond = [NSString stringWithFormat:@"%ld", (long)[callBackdate timeIntervalSince1970]];
    int secondTimeStamp=[strSecond intValue];
    
    int datePoor = firstTimeStamp-secondTimeStamp;
    return datePoor;
}
//时间字符串转yyyy-MM-dd HH:mm:ss
+ (NSString *)callBackDateTime:(NSString *)timeString
{
//    //获得当前时间
    NSDate *currentDate=[NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
    NSTimeInterval  timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];
    currentDate = [currentDate dateByAddingTimeInterval:timeZoneOffset];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
//    NSString *strFirst = [NSString stringWithFormat:@"%ld", (long)[currentDate timeIntervalSince1970]];
//    int firstTimeStamp=[strFirst intValue];

    NSString *dateTime = timeString;
//    NSString *dateTime = [Global timeWithTimeIntervalString:timeString];
//    //获取服务器返回时间
//    NSDate *callBackdate =[dateFormatter dateFromString:dateTime];
//    callBackdate = [callBackdate dateByAddingTimeInterval:timeZoneOffset];
//    NSString *strSecond = [NSString stringWithFormat:@"%ld", (long)[callBackdate timeIntervalSince1970]];
//    int secondTimeStamp=[strSecond intValue];
    
    int datePoor = [Global callDatePoor:timeString];
    
    if (datePoor<0)
    {
        dateTime=@"刚刚";
    }
    else if (0<=datePoor && datePoor<60)
    {
        dateTime=[NSString stringWithFormat:@"%d秒前",datePoor];
    }
    else if (60<=datePoor && datePoor<60*60)
    {
        dateTime=[NSString stringWithFormat:@"%d分钟前",datePoor/60];
    }
    else if (60*60<=datePoor && datePoor<24*60*60)
    {
        dateTime=[NSString stringWithFormat:@"%d小时前",datePoor/60/60];
    }
    else if (60*60<=datePoor && datePoor<24*60*60)
    {
        dateTime=[NSString stringWithFormat:@"%d小时前",datePoor/60/60];
    }
    else
    {
        dateTime = [NSString stringWithFormat:@"%@月%@日 %@",[dateTime substringWithRange:NSMakeRange(5,2)],[dateTime substringWithRange:NSMakeRange(8,2)],[dateTime substringWithRange:NSMakeRange(11, 5)]];
        
    }
    return dateTime;
}
//时间字符串转yyyy-MM-dd
+ (NSString *)callBackDate:(NSString *)dateTime
{
    //获得当前时间
    NSDate *currentDate=[NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];
    NSTimeInterval  timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];
    currentDate = [currentDate dateByAddingTimeInterval:timeZoneOffset];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *strFirst = [NSString stringWithFormat:@"%ld", (long)[currentDate timeIntervalSince1970]];
    int firstTimeStamp=[strFirst intValue];
    
    NSString *weakDay = [Global getWeekDayFordate:dateTime];
    //获取服务器返回时间
    NSDate *callBackdate =[dateFormatter dateFromString:dateTime];
    callBackdate = [callBackdate dateByAddingTimeInterval:timeZoneOffset];
    NSString *strSecond = [NSString stringWithFormat:@"%ld", (long)[callBackdate timeIntervalSince1970]];
    int secondTimeStamp=[strSecond intValue];
    
    int datePoor = firstTimeStamp-secondTimeStamp;
    if (60*60<=datePoor && datePoor<24*60*60)
    {
        dateTime=[NSString stringWithFormat:@"今天 %@月%@日",[dateTime substringWithRange:NSMakeRange(5,2)],[dateTime substringWithRange:NSMakeRange(8,2)]];
    }else if (24*60*60<=datePoor && datePoor<48*60*60)
    {
        dateTime=[NSString stringWithFormat:@"昨天 %@月%@日",[dateTime substringWithRange:NSMakeRange(5,2)],[dateTime substringWithRange:NSMakeRange(8,2)]];
    }
    else
    {
        if (dateTime.length>10)
        {
            dateTime = [dateTime substringToIndex:10];
        }
        dateTime = [NSString stringWithFormat:@"%@年%@月%@日",[dateTime substringWithRange:NSMakeRange(0,4)],[dateTime substringWithRange:NSMakeRange(5,2)],[dateTime substringWithRange:NSMakeRange(8,2)]];
    }
    return [NSString stringWithFormat:@"%@ %@",dateTime,weakDay];
}
//时间戳转化为时间
+ (NSDate *)timeWithTimeIntervalDate:(NSString *)timeString
{
    // 格式化时间
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    // 毫秒值转化为秒
    NSDate* date = [NSDate dateWithTimeIntervalSince1970:[timeString doubleValue]/ 1000.0];
    return date;
}
//时间戳转化为时间字符串
+ (NSString *)timeWithTimeIntervalString:(NSString *)timeString
{
    NSDateFormatter*formatter = [[NSDateFormatter alloc]init];
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    
    int timeval = [timeString intValue];
    
    NSDate*confromTimesp = [NSDate dateWithTimeIntervalSince1970:timeval];
    
    // NSLog(@"1296035591  = %@",confromTimesp);
    
    NSString*confromTimespStr = [formatter stringFromDate:confromTimesp];
    
    return confromTimespStr;
    //    return dateString;
}
//根据时间字符串获取星期几
+ (NSString *)getWeekDayFordate:(NSString *)strDate
{
    NSArray *weekday = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [formatter dateFromString:strDate];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [calendar components:NSCalendarUnitWeekday fromDate:date];
    
    NSString *weekStr = [weekday objectAtIndex:components.weekday];
    return weekStr;
}
//获取当前时间YYYY-MM-dd
+ (NSString *)getCurrentDate
{
    //系统时间
    NSDate *localDate=[NSDate date];
    MyLog(@"localDate%@",localDate);
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    //    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
    [dateFormatter setDateFormat:@"YYYY-MM-dd"];
    //转化为实际的时间
    //    NSTimeZone *zone = [NSTimeZone systemTimeZone];     //系统时区
    //    NSInteger interval = [zone secondsFromGMTForDate: localDate];
    //    NSDate *localeDate = [localDate  dateByAddingTimeInterval: interval];
    NSString *lacalStr=[dateFormatter stringFromDate:localDate];
    MyLog(@"lacalStr%@",lacalStr);
    return lacalStr;
}
//获取当前时间YYYY-MM-dd HH:mm:ss
+ (NSString *)getCurrentDateTime
{
    //系统时间
    NSDate *localDate=[NSDate date];
    MyLog(@"localDate%@",localDate);
    NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
    dateFormatter.timeZone = [NSTimeZone timeZoneWithName:@"beijing"];
    [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    //转化为实际的时间
    //    NSTimeZone *zone = [NSTimeZone systemTimeZone];     //系统时区
    //    NSInteger interval = [zone secondsFromGMTForDate: localDate];
    //    NSDate *localeDate = [localDate  dateByAddingTimeInterval: interval];
    NSString *lacalStr=[dateFormatter stringFromDate:localDate];
    MyLog(@"lacalStr%@",lacalStr);
    return lacalStr;
}


@end
